import { SetMetadata } from "@nestjs/common";
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Request } from "express";

//自定义装饰器，用于判断用户是否登录
export const  RequireLogin = () => SetMetadata('require-login', true);


//自定义装饰器，用于获取用户信息
export const UserInfo = createParamDecorator(
    (data: string, ctx: ExecutionContext) => {
        //通过ExecutionContext获取请求对象
      const request = ctx.switchToHttp().getRequest<Request>();
    
      //判断用户是否登录
      if(!request.user) {
          return null;
      }
      
      return data ? request.user[data] : request.user;
    },
  );